package com.yibai.web.app.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yibai.web.app.entity.Article;
import com.yibai.web.app.mapper.ArticleMapper;
import com.yibai.web.app.service.ArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yibai.web.dto.ArticleInfoRequest;
import com.yibai.web.dto.ArticleListRequest;
import com.yibai.web.dto.ArticleRequest;
import com.yibai.web.dto.SuperResponse;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author fang
 * @since 2021-04-19
 */
@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService {


    @Override
    public SuperResponse list(ArticleListRequest request) {
        SuperResponse response = new SuperResponse();
        QueryWrapper<Article> wrapper = new QueryWrapper();
        Page<Article> pages = new Page<>(request.getPageNo(),request.getPageSize());
        IPage<Article> mapIPage = baseMapper.selectPage(pages, wrapper);
        List<Article> agents = mapIPage.getRecords();
        response.setItems(agents);
        response.setTotals(pages.getTotal());
        return response;
    }

    @Override
    public SuperResponse info(ArticleRequest request) {
        SuperResponse response = new SuperResponse();
        response.setItem(baseMapper.selectById(request.getId()));
        return response;
    }

    @Override
    public SuperResponse add(ArticleInfoRequest request) {
        SuperResponse response = new SuperResponse();
        Article article = new Article();
        article.setAuthorId(request.getAuthorId());
        article.setCid(request.getCid());
        article.setSiteId(request.getSiteId());
        article.setTitle(request.getTitle());
        article.setSubtitle(request.getSubtitle());
        article.setKeywords(request.getKeywords());
        article.setContents(request.getContents());
        article.setImageUrl(request.getImageUrl());
        article.setOrigin(request.getOrigin());
        article.setType(request.getType());
        article.setPosition(request.getPosition());
        article.setClicks(request.getClicks());
        baseMapper.insert(article);
        return response;
    }

    @Override
    public SuperResponse update(ArticleInfoRequest request) {
        SuperResponse response = new SuperResponse();
        Article article = new Article();
        article.setId(request.getId());
        article.setAuthorId(request.getAuthorId());
        article.setCid(request.getCid());
        article.setSiteId(request.getSiteId());
        article.setTitle(request.getTitle());
        article.setSubtitle(request.getSubtitle());
        article.setKeywords(request.getKeywords());
        article.setContents(request.getContents());
        article.setImageUrl(request.getImageUrl());
        article.setOrigin(request.getOrigin());
        article.setType(request.getType());
        article.setPosition(request.getPosition());
        article.setClicks(request.getClicks());
        baseMapper.updateById(article);
        return response;
    }

    @Override
    public SuperResponse delete(ArticleRequest request) {
        SuperResponse response = new SuperResponse();
        baseMapper.deleteById(request.getId());
        return response;
    }


}
